package com.bkhech.satoken.controller.h2test;

import com.bkhech.satoken.domain.Item;
import com.bkhech.satoken.mapper.ItemRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * H2 test controller
 *
 * @author guowm
 * @date 2023/8/14
 */
@RequiredArgsConstructor
@RestController
public class ItemController {
    final ItemRepository itemRepo;

    @RequestMapping("/getAllItems")
    public List<Item> getAllItems() {
        return itemRepo.findAll();
    }

    @RequestMapping("/getItem")
    public Item getItem(@RequestParam("itemId") int itemId) {
        return itemRepo.findById(itemId).orElse(null);
    }

    @RequestMapping("/addItem")
    public String addItem(@RequestParam("id") int id, @RequestParam("name") String name,
                          @RequestParam("category") String category) {
        itemRepo.save(Item.builder().id(id).name(name).category(category).build());
        return "Item Added Successfully";
    }

    @RequestMapping("/deleteItem")
    public String deteteItem(@RequestParam("itemId") int itemId) {
        itemRepo.deleteById(itemId);
        return "Item Deleted Successfully";
    }
}